package meituan.d20210904.t2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

/**
 * @author habitplus
 * @since 2021-09-04 14:38
 */
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String[] ss = bf.readLine().split(" ");
        int n = Integer.parseInt(ss[0]);
        int m = Integer.parseInt(ss[1]);
        int k, line;
        Map<Integer, List<Integer>> lineMap = new HashMap<>();
        Map<Integer, List<Integer>> busMap = new HashMap<>();
        for (int i = 0; i < n; i++) {
            ss = bf.readLine().split(" ");
            k = Integer.parseInt(ss[0]);
            if (k > 0) {
                List<Integer> list = new ArrayList<>();
                for (int j = 1; j <= k; j++) {
                    line = Integer.parseInt(ss[j]);
                    list.add(line);
                    if (lineMap.containsKey(line)) {
                        lineMap.get(line).add(i);
                    } else {
                        List<Integer> tmp = new ArrayList<>();
                        tmp.add(i);
                        lineMap.put(line, tmp);
                    }
                }
                busMap.put(i, list);
            }
        }

        int[][] ans = computeLine(n, m, busMap, lineMap);

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < ans.length; i++) {
            for (int j = 0; j < ans[0].length; j++) {
                k = i > j ? ans[j][i] : ans[i][j];
                k = i != j && k == 0 ? -1 : k;
                sb.append(k);
                if (j != ans[0].length - 1) {
                    sb.append(" ");
                }
            }
            sb.append("\n");
        }

        System.out.println(sb.toString());
    }

    private static int[][] computeLine(int n, int m, Map<Integer, List<Integer>> busMap, Map<Integer, List<Integer>> lineMap) {
        if (n < 1) {
            return new int[0][];
        }

        int[][] ans = new int[n][n];
        // 对每个车站进行 bfs
        for (int i = 0; i < n; i++) {
            // 当前车站有公交到达
            if (busMap.containsKey(i)) {
                bfs(busMap, lineMap, m, i, ans);
            }
        }
        return ans;
    }

    private static void bfs(Map<Integer, List<Integer>> busMap, Map<Integer, List<Integer>> lineMap, int m, int busNo, int[][] dis) {
        Queue<Integer> que = new LinkedList<>();
        // 当前站点的所有公交线入队
        que.addAll(busMap.get(busNo));
        boolean[] v = new boolean[m + 1];
        int line;
        int cnt = 0;
        while (!que.isEmpty()) {
            cnt++;
            for (int i = que.size(); i > 0; i--) {
                line = que.poll();
                // 该条公交线已经遍历过
                v[line] = true;
                // 判断当前线路所到的车站
                if (lineMap.containsKey(line)) {
                    List<Integer> station = lineMap.get(line);

                    for (int st : station) {
                        // 当前站点还没计算过
                        if (st > busNo && dis[busNo][st] == 0 && dis[st][busNo] == 0) {
                            dis[busNo][st] = cnt;

                            // 所有站点的公交线全部入队
                            for (int l : busMap.get(st)) {
                                if (!v[l]) que.add(l);
                            }
                        }
                    }
                }
            }
        }
    }
}
